package the.workshop.bu.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.wisteria.framework.util.SerializeUtil;
import org.wisteria.framework.web.mvc.service.AbstractSuperService;
import the.workshop.bu.constant.CheckBizTypeEnum;
import the.workshop.bu.constant.CheckSourceEnum;
import the.workshop.bu.constant.CheckTypeEnum;
import the.workshop.bu.dto.MobileEmptyDTO;
import the.workshop.bu.dto.MobileNameDTO;
import the.workshop.bu.dto.MobileStatusDTO;
import the.workshop.bu.dto.ServiceResult;
import the.workshop.bu.entity.Check;
import the.workshop.bu.mapper.CheckMapper;
import the.workshop.bu.remote.MobileCheckComponent;
import the.workshop.bu.service.ICheckService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

@Service
public class CheckServiceImpl extends AbstractSuperService<CheckMapper, Check> implements ICheckService {

    private static final int DEFAULT_CHECK_SOURCE = CheckSourceEnum.Tianyan.getValue();

    @Resource
    private MobileCheckComponent mobileCheckComponent;

    @Override
    public ServiceResult<MobileEmptyDTO> checkMobileEmpty(CheckBizTypeEnum checkBizType, long bizId, String mobile, Map<String, Object> checkData) {
        return checkMobile(CheckTypeEnum.MOBILE_EMPTY, checkBizType, bizId, mobile, null, checkData);
    }

    @Override
    public ServiceResult<MobileStatusDTO> checkMobileStatus(CheckBizTypeEnum checkBizType, long bizId, String mobile) {
        return checkMobile(CheckTypeEnum.MOBILE_STATUS, checkBizType, bizId, mobile, null, null);
    }

    @Override
    public ServiceResult<MobileNameDTO> checkMobileAndName(CheckBizTypeEnum checkBizType, long bizId, String mobile, String name, Map<String, Object> checkData) {
        return checkMobile(CheckTypeEnum.MOBILE_NAME, checkBizType, bizId, mobile, name, checkData);
    }

    private ServiceResult checkMobile(CheckTypeEnum checkType, CheckBizTypeEnum checkBizType, long bizId, String mobile, String name, Map<String, Object> checkData) {
        if (!StringUtils.hasText(mobile)) {
            return null;
        }

        ServiceResult result = null;
        if (CheckTypeEnum.MOBILE_EMPTY.equals(checkType)) {
            result = mobileCheckComponent.checkEmpty(mobile);
        } else if (CheckTypeEnum.MOBILE_STATUS.equals(checkType)) {
            result = mobileCheckComponent.checkStatus(mobile);
        } else if (CheckTypeEnum.MOBILE_NAME.equals(checkType) && StringUtils.hasText(name)) {
            result = mobileCheckComponent.checkMobileAndName(mobile, name);
        }
        if (result == null) {
            return null;
        }

        Check entity = new Check();
        entity.setCheckSource(DEFAULT_CHECK_SOURCE);
        entity.setCheckType(checkType.getValue());
        entity.setBizType(checkBizType.getValue());
        entity.setBizId(bizId);
        entity.setCheckTime(new Date());
        entity.setCheckData(SerializeUtil.serialize(checkData));
        entity.setCheckResult(SerializeUtil.serialize(result));
        if (result.getSuccess() || result.getCode().intValue() == 1003) {
            entity.setCheckStatus(0);
        } else {
            entity.setCheckStatus(1);
        }
        this.save(entity);
        return result;
    }

}
